
USE [UserDefinedForms]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the ControlTypes table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_Get_List

AS


				
				SELECT
					[ControlTypeID],
					[Name],
					[Status],
					[Description]
				FROM
					[dbo].[ControlTypes]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the ControlTypes table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ControlTypeID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ControlTypeID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ControlTypeID]'
				SET @SQL = @SQL + ' FROM [dbo].[ControlTypes]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ControlTypeID], O.[Name], O.[Status], O.[Description]
				FROM
				    [dbo].[ControlTypes] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ControlTypeID] = PageIndex.[ControlTypeID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ControlTypes]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the ControlTypes table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_Insert
(

	@ControlTypeId int    OUTPUT,

	@Name nvarchar (50)  ,

	@Status int   ,

	@Description nvarchar (1000)  
)
AS


				
				INSERT INTO [dbo].[ControlTypes]
					(
					[Name]
					,[Status]
					,[Description]
					)
				VALUES
					(
					@Name
					,@Status
					,@Description
					)
				
				-- Get the identity value
				SET @ControlTypeId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the ControlTypes table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_Update
(

	@ControlTypeId int   ,

	@Name nvarchar (50)  ,

	@Status int   ,

	@Description nvarchar (1000)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ControlTypes]
				SET
					[Name] = @Name
					,[Status] = @Status
					,[Description] = @Description
				WHERE
[ControlTypeID] = @ControlTypeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the ControlTypes table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_Delete
(

	@ControlTypeId int   
)
AS


				DELETE FROM [dbo].[ControlTypes] WITH (ROWLOCK) 
				WHERE
					[ControlTypeID] = @ControlTypeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_GetByControlTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_GetByControlTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_GetByControlTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the ControlTypes table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_GetByControlTypeId
(

	@ControlTypeId int   
)
AS


				SELECT
					[ControlTypeID],
					[Name],
					[Status],
					[Description]
				FROM
					[dbo].[ControlTypes]
				WHERE
					[ControlTypeID] = @ControlTypeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlTypes_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlTypes_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlTypes_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the ControlTypes table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlTypes_Find
(

	@SearchUsingOR bit   = null ,

	@ControlTypeId int   = null ,

	@Name nvarchar (50)  = null ,

	@Status int   = null ,

	@Description nvarchar (1000)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ControlTypeID]
	, [Name]
	, [Status]
	, [Description]
    FROM
	[dbo].[ControlTypes]
    WHERE 
	 ([ControlTypeID] = @ControlTypeId OR @ControlTypeId IS NULL)
	AND ([Name] = @Name OR @Name IS NULL)
	AND ([Status] = @Status OR @Status IS NULL)
	AND ([Description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ControlTypeID]
	, [Name]
	, [Status]
	, [Description]
    FROM
	[dbo].[ControlTypes]
    WHERE 
	 ([ControlTypeID] = @ControlTypeId AND @ControlTypeId is not null)
	OR ([Name] = @Name AND @Name is not null)
	OR ([Status] = @Status AND @Status is not null)
	OR ([Description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_Get_List

AS


				
				SELECT
					[UserID],
					[UserName],
					[Type],
					[Password],
					[Status]
				FROM
					[dbo].[Users]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the Users table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [UserID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([UserID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [UserID]'
				SET @SQL = @SQL + ' FROM [dbo].[Users]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[UserID], O.[UserName], O.[Type], O.[Password], O.[Status]
				FROM
				    [dbo].[Users] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[UserID] = PageIndex.[UserID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Users]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_Insert
(

	@UserId int    OUTPUT,

	@UserName nvarchar (200)  ,

	@Type int   ,

	@Password nvarchar (200)  ,

	@Status int   
)
AS


				
				INSERT INTO [dbo].[Users]
					(
					[UserName]
					,[Type]
					,[Password]
					,[Status]
					)
				VALUES
					(
					@UserName
					,@Type
					,@Password
					,@Status
					)
				
				-- Get the identity value
				SET @UserId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_Update
(

	@UserId int   ,

	@UserName nvarchar (200)  ,

	@Type int   ,

	@Password nvarchar (200)  ,

	@Status int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Users]
				SET
					[UserName] = @UserName
					,[Type] = @Type
					,[Password] = @Password
					,[Status] = @Status
				WHERE
[UserID] = @UserId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the Users table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_Delete
(

	@UserId int   
)
AS


				DELETE FROM [dbo].[Users] WITH (ROWLOCK) 
				WHERE
					[UserID] = @UserId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_GetByUserId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_GetByUserId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_GetByUserId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the Users table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_GetByUserId
(

	@UserId int   
)
AS


				SELECT
					[UserID],
					[UserName],
					[Type],
					[Password],
					[Status]
				FROM
					[dbo].[Users]
				WHERE
					[UserID] = @UserId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Users_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Users_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Users_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the Users table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Users_Find
(

	@SearchUsingOR bit   = null ,

	@UserId int   = null ,

	@UserName nvarchar (200)  = null ,

	@Type int   = null ,

	@Password nvarchar (200)  = null ,

	@Status int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [UserID]
	, [UserName]
	, [Type]
	, [Password]
	, [Status]
    FROM
	[dbo].[Users]
    WHERE 
	 ([UserID] = @UserId OR @UserId IS NULL)
	AND ([UserName] = @UserName OR @UserName IS NULL)
	AND ([Type] = @Type OR @Type IS NULL)
	AND ([Password] = @Password OR @Password IS NULL)
	AND ([Status] = @Status OR @Status IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [UserID]
	, [UserName]
	, [Type]
	, [Password]
	, [Status]
    FROM
	[dbo].[Users]
    WHERE 
	 ([UserID] = @UserId AND @UserId is not null)
	OR ([UserName] = @UserName AND @UserName is not null)
	OR ([Type] = @Type AND @Type is not null)
	OR ([Password] = @Password AND @Password is not null)
	OR ([Status] = @Status AND @Status is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the DesignForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_Get_List

AS


				
				SELECT
					[FormID],
					[Name],
					[Status],
					[Description],
					[EffectiveDate],
					[CreatedDate],
					[ModifiedDate],
					[UserID],
					[FormType]
				FROM
					[dbo].[DesignForms]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the DesignForms table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [FormID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([FormID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [FormID]'
				SET @SQL = @SQL + ' FROM [dbo].[DesignForms]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[FormID], O.[Name], O.[Status], O.[Description], O.[EffectiveDate], O.[CreatedDate], O.[ModifiedDate], O.[UserID], O.[FormType]
				FROM
				    [dbo].[DesignForms] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[FormID] = PageIndex.[FormID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[DesignForms]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the DesignForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_Insert
(

	@FormId int    OUTPUT,

	@Name nvarchar (200)  ,

	@Status int   ,

	@Description nvarchar (1000)  ,

	@EffectiveDate datetime   ,

	@CreatedDate datetime   ,

	@ModifiedDate datetime   ,

	@UserId int   ,

	@FormType int   
)
AS


				
				INSERT INTO [dbo].[DesignForms]
					(
					[Name]
					,[Status]
					,[Description]
					,[EffectiveDate]
					,[CreatedDate]
					,[ModifiedDate]
					,[UserID]
					,[FormType]
					)
				VALUES
					(
					@Name
					,@Status
					,@Description
					,@EffectiveDate
					,@CreatedDate
					,@ModifiedDate
					,@UserId
					,@FormType
					)
				
				-- Get the identity value
				SET @FormId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the DesignForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_Update
(

	@FormId int   ,

	@Name nvarchar (200)  ,

	@Status int   ,

	@Description nvarchar (1000)  ,

	@EffectiveDate datetime   ,

	@CreatedDate datetime   ,

	@ModifiedDate datetime   ,

	@UserId int   ,

	@FormType int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[DesignForms]
				SET
					[Name] = @Name
					,[Status] = @Status
					,[Description] = @Description
					,[EffectiveDate] = @EffectiveDate
					,[CreatedDate] = @CreatedDate
					,[ModifiedDate] = @ModifiedDate
					,[UserID] = @UserId
					,[FormType] = @FormType
				WHERE
[FormID] = @FormId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the DesignForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_Delete
(

	@FormId int   
)
AS


				DELETE FROM [dbo].[DesignForms] WITH (ROWLOCK) 
				WHERE
					[FormID] = @FormId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_GetByUserId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_GetByUserId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_GetByUserId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the DesignForms table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_GetByUserId
(

	@UserId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[FormID],
					[Name],
					[Status],
					[Description],
					[EffectiveDate],
					[CreatedDate],
					[ModifiedDate],
					[UserID],
					[FormType]
				FROM
					[dbo].[DesignForms]
				WHERE
					[UserID] = @UserId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_GetByFormId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_GetByFormId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_GetByFormId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the DesignForms table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_GetByFormId
(

	@FormId int   
)
AS


				SELECT
					[FormID],
					[Name],
					[Status],
					[Description],
					[EffectiveDate],
					[CreatedDate],
					[ModifiedDate],
					[UserID],
					[FormType]
				FROM
					[dbo].[DesignForms]
				WHERE
					[FormID] = @FormId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.DesignForms_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.DesignForms_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.DesignForms_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the DesignForms table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.DesignForms_Find
(

	@SearchUsingOR bit   = null ,

	@FormId int   = null ,

	@Name nvarchar (200)  = null ,

	@Status int   = null ,

	@Description nvarchar (1000)  = null ,

	@EffectiveDate datetime   = null ,

	@CreatedDate datetime   = null ,

	@ModifiedDate datetime   = null ,

	@UserId int   = null ,

	@FormType int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [FormID]
	, [Name]
	, [Status]
	, [Description]
	, [EffectiveDate]
	, [CreatedDate]
	, [ModifiedDate]
	, [UserID]
	, [FormType]
    FROM
	[dbo].[DesignForms]
    WHERE 
	 ([FormID] = @FormId OR @FormId IS NULL)
	AND ([Name] = @Name OR @Name IS NULL)
	AND ([Status] = @Status OR @Status IS NULL)
	AND ([Description] = @Description OR @Description IS NULL)
	AND ([EffectiveDate] = @EffectiveDate OR @EffectiveDate IS NULL)
	AND ([CreatedDate] = @CreatedDate OR @CreatedDate IS NULL)
	AND ([ModifiedDate] = @ModifiedDate OR @ModifiedDate IS NULL)
	AND ([UserID] = @UserId OR @UserId IS NULL)
	AND ([FormType] = @FormType OR @FormType IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [FormID]
	, [Name]
	, [Status]
	, [Description]
	, [EffectiveDate]
	, [CreatedDate]
	, [ModifiedDate]
	, [UserID]
	, [FormType]
    FROM
	[dbo].[DesignForms]
    WHERE 
	 ([FormID] = @FormId AND @FormId is not null)
	OR ([Name] = @Name AND @Name is not null)
	OR ([Status] = @Status AND @Status is not null)
	OR ([Description] = @Description AND @Description is not null)
	OR ([EffectiveDate] = @EffectiveDate AND @EffectiveDate is not null)
	OR ([CreatedDate] = @CreatedDate AND @CreatedDate is not null)
	OR ([ModifiedDate] = @ModifiedDate AND @ModifiedDate is not null)
	OR ([UserID] = @UserId AND @UserId is not null)
	OR ([FormType] = @FormType AND @FormType is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the SupOrderForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_Get_List

AS


				
				SELECT
					[FormID],
					[Background],
					[SupplierID]
				FROM
					[dbo].[SupOrderForms]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the SupOrderForms table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [FormID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([FormID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [FormID]'
				SET @SQL = @SQL + ' FROM [dbo].[SupOrderForms]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[FormID], O.[Background], O.[SupplierID]
				FROM
				    [dbo].[SupOrderForms] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[FormID] = PageIndex.[FormID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[SupOrderForms]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the SupOrderForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_Insert
(

	@FormId int   ,

	@Background image   ,

	@SupplierId int   
)
AS


				
				INSERT INTO [dbo].[SupOrderForms]
					(
					[FormID]
					,[Background]
					,[SupplierID]
					)
				VALUES
					(
					@FormId
					,@Background
					,@SupplierId
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the SupOrderForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_Update
(

	@FormId int   ,

	@OriginalFormId int   ,

	@Background image   ,

	@SupplierId int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[SupOrderForms]
				SET
					[FormID] = @FormId
					,[Background] = @Background
					,[SupplierID] = @SupplierId
				WHERE
[FormID] = @OriginalFormId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the SupOrderForms table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_Delete
(

	@FormId int   
)
AS


				DELETE FROM [dbo].[SupOrderForms] WITH (ROWLOCK) 
				WHERE
					[FormID] = @FormId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_GetByFormId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_GetByFormId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_GetByFormId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the SupOrderForms table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_GetByFormId
(

	@FormId int   
)
AS


				SELECT
					[FormID],
					[Background],
					[SupplierID]
				FROM
					[dbo].[SupOrderForms]
				WHERE
					[FormID] = @FormId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_GetBySupplierId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_GetBySupplierId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_GetBySupplierId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the SupOrderForms table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_GetBySupplierId
(

	@SupplierId int   
)
AS


				SELECT
					[FormID],
					[Background],
					[SupplierID]
				FROM
					[dbo].[SupOrderForms]
				WHERE
					[SupplierID] = @SupplierId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.SupOrderForms_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.SupOrderForms_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.SupOrderForms_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the SupOrderForms table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.SupOrderForms_Find
(

	@SearchUsingOR bit   = null ,

	@FormId int   = null ,

	@Background image   = null ,

	@SupplierId int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [FormID]
	, [Background]
	, [SupplierID]
    FROM
	[dbo].[SupOrderForms]
    WHERE 
	 ([FormID] = @FormId OR @FormId IS NULL)
	AND ([SupplierID] = @SupplierId OR @SupplierId IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [FormID]
	, [Background]
	, [SupplierID]
    FROM
	[dbo].[SupOrderForms]
    WHERE 
	 ([FormID] = @FormId AND @FormId is not null)
	OR ([SupplierID] = @SupplierId AND @SupplierId is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the FormControls table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_Get_List

AS


				
				SELECT
					[ControlID],
					[Name],
					[ControlTypeID],
					[ToolTipText],
					[Border],
					[Top],
					[Left],
					[Width],
					[Height],
					[Font],
					[DefaultValue],
					[Visible],
					[Enable],
					[IsSummaryField],
					[IsCommon],
					[IsCode],
					[IsLayout],
					[ComponentID],
					[FormID],
					[FormType],
					[Status],
					[Description]
				FROM
					[dbo].[FormControls]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the FormControls table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ControlID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ControlID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ControlID]'
				SET @SQL = @SQL + ' FROM [dbo].[FormControls]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ControlID], O.[Name], O.[ControlTypeID], O.[ToolTipText], O.[Border], O.[Top], O.[Left], O.[Width], O.[Height], O.[Font], O.[DefaultValue], O.[Visible], O.[Enable], O.[IsSummaryField], O.[IsCommon], O.[IsCode], O.[IsLayout], O.[ComponentID], O.[FormID], O.[FormType], O.[Status], O.[Description]
				FROM
				    [dbo].[FormControls] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ControlID] = PageIndex.[ControlID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[FormControls]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the FormControls table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_Insert
(

	@ControlId int    OUTPUT,

	@Name nvarchar (100)  ,

	@ControlTypeId int   ,

	@ToolTipText nvarchar (1000)  ,

	@Border bit   ,

	@Top int   ,

	@Left int   ,

	@Width int   ,

	@Height int   ,

	@Font nvarchar (50)  ,

	@DefaultValue nvarchar (200)  ,

	@Visible bit   ,

	@Enable bit   ,

	@IsSummaryField bit   ,

	@IsCommon bit   ,

	@IsCode bit   ,

	@IsLayout bit   ,

	@ComponentId int   ,

	@FormId int   ,

	@FormType int   ,

	@Status int   ,

	@Description nvarchar (1000)  
)
AS


				
				INSERT INTO [dbo].[FormControls]
					(
					[Name]
					,[ControlTypeID]
					,[ToolTipText]
					,[Border]
					,[Top]
					,[Left]
					,[Width]
					,[Height]
					,[Font]
					,[DefaultValue]
					,[Visible]
					,[Enable]
					,[IsSummaryField]
					,[IsCommon]
					,[IsCode]
					,[IsLayout]
					,[ComponentID]
					,[FormID]
					,[FormType]
					,[Status]
					,[Description]
					)
				VALUES
					(
					@Name
					,@ControlTypeId
					,@ToolTipText
					,@Border
					,@Top
					,@Left
					,@Width
					,@Height
					,@Font
					,@DefaultValue
					,@Visible
					,@Enable
					,@IsSummaryField
					,@IsCommon
					,@IsCode
					,@IsLayout
					,@ComponentId
					,@FormId
					,@FormType
					,@Status
					,@Description
					)
				
				-- Get the identity value
				SET @ControlId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the FormControls table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_Update
(

	@ControlId int   ,

	@Name nvarchar (100)  ,

	@ControlTypeId int   ,

	@ToolTipText nvarchar (1000)  ,

	@Border bit   ,

	@Top int   ,

	@Left int   ,

	@Width int   ,

	@Height int   ,

	@Font nvarchar (50)  ,

	@DefaultValue nvarchar (200)  ,

	@Visible bit   ,

	@Enable bit   ,

	@IsSummaryField bit   ,

	@IsCommon bit   ,

	@IsCode bit   ,

	@IsLayout bit   ,

	@ComponentId int   ,

	@FormId int   ,

	@FormType int   ,

	@Status int   ,

	@Description nvarchar (1000)  
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[FormControls]
				SET
					[Name] = @Name
					,[ControlTypeID] = @ControlTypeId
					,[ToolTipText] = @ToolTipText
					,[Border] = @Border
					,[Top] = @Top
					,[Left] = @Left
					,[Width] = @Width
					,[Height] = @Height
					,[Font] = @Font
					,[DefaultValue] = @DefaultValue
					,[Visible] = @Visible
					,[Enable] = @Enable
					,[IsSummaryField] = @IsSummaryField
					,[IsCommon] = @IsCommon
					,[IsCode] = @IsCode
					,[IsLayout] = @IsLayout
					,[ComponentID] = @ComponentId
					,[FormID] = @FormId
					,[FormType] = @FormType
					,[Status] = @Status
					,[Description] = @Description
				WHERE
[ControlID] = @ControlId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the FormControls table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_Delete
(

	@ControlId int   
)
AS


				DELETE FROM [dbo].[FormControls] WITH (ROWLOCK) 
				WHERE
					[ControlID] = @ControlId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_GetByControlTypeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_GetByControlTypeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_GetByControlTypeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormControls table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_GetByControlTypeId
(

	@ControlTypeId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ControlID],
					[Name],
					[ControlTypeID],
					[ToolTipText],
					[Border],
					[Top],
					[Left],
					[Width],
					[Height],
					[Font],
					[DefaultValue],
					[Visible],
					[Enable],
					[IsSummaryField],
					[IsCommon],
					[IsCode],
					[IsLayout],
					[ComponentID],
					[FormID],
					[FormType],
					[Status],
					[Description]
				FROM
					[dbo].[FormControls]
				WHERE
					[ControlTypeID] = @ControlTypeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_GetByFormId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_GetByFormId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_GetByFormId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormControls table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_GetByFormId
(

	@FormId int   
)
AS


				SELECT
					[ControlID],
					[Name],
					[ControlTypeID],
					[ToolTipText],
					[Border],
					[Top],
					[Left],
					[Width],
					[Height],
					[Font],
					[DefaultValue],
					[Visible],
					[Enable],
					[IsSummaryField],
					[IsCommon],
					[IsCode],
					[IsLayout],
					[ComponentID],
					[FormID],
					[FormType],
					[Status],
					[Description]
				FROM
					[dbo].[FormControls]
				WHERE
					[FormID] = @FormId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_GetByComponentId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_GetByComponentId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_GetByComponentId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormControls table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_GetByComponentId
(

	@ComponentId int   
)
AS


				SELECT
					[ControlID],
					[Name],
					[ControlTypeID],
					[ToolTipText],
					[Border],
					[Top],
					[Left],
					[Width],
					[Height],
					[Font],
					[DefaultValue],
					[Visible],
					[Enable],
					[IsSummaryField],
					[IsCommon],
					[IsCode],
					[IsLayout],
					[ComponentID],
					[FormID],
					[FormType],
					[Status],
					[Description]
				FROM
					[dbo].[FormControls]
				WHERE
					[ComponentID] = @ComponentId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_GetByControlId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_GetByControlId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_GetByControlId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormControls table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_GetByControlId
(

	@ControlId int   
)
AS


				SELECT
					[ControlID],
					[Name],
					[ControlTypeID],
					[ToolTipText],
					[Border],
					[Top],
					[Left],
					[Width],
					[Height],
					[Font],
					[DefaultValue],
					[Visible],
					[Enable],
					[IsSummaryField],
					[IsCommon],
					[IsCode],
					[IsLayout],
					[ComponentID],
					[FormID],
					[FormType],
					[Status],
					[Description]
				FROM
					[dbo].[FormControls]
				WHERE
					[ControlID] = @ControlId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_GetBySupplierIdFromComponentSupplier procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_GetBySupplierIdFromComponentSupplier') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_GetBySupplierIdFromComponentSupplier
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records through a junction table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_GetBySupplierIdFromComponentSupplier
(

	@SupplierId int   
)
AS


SELECT dbo.[FormControls].[ControlID]
       ,dbo.[FormControls].[Name]
       ,dbo.[FormControls].[ControlTypeID]
       ,dbo.[FormControls].[ToolTipText]
       ,dbo.[FormControls].[Border]
       ,dbo.[FormControls].[Top]
       ,dbo.[FormControls].[Left]
       ,dbo.[FormControls].[Width]
       ,dbo.[FormControls].[Height]
       ,dbo.[FormControls].[Font]
       ,dbo.[FormControls].[DefaultValue]
       ,dbo.[FormControls].[Visible]
       ,dbo.[FormControls].[Enable]
       ,dbo.[FormControls].[IsSummaryField]
       ,dbo.[FormControls].[IsCommon]
       ,dbo.[FormControls].[IsCode]
       ,dbo.[FormControls].[IsLayout]
       ,dbo.[FormControls].[ComponentID]
       ,dbo.[FormControls].[FormID]
       ,dbo.[FormControls].[FormType]
       ,dbo.[FormControls].[Status]
       ,dbo.[FormControls].[Description]
  FROM dbo.[FormControls]
 WHERE EXISTS (SELECT 1
                 FROM dbo.[ComponentSuppliers] 
                WHERE dbo.[ComponentSuppliers].[SupplierID] = @SupplierId
                  AND dbo.[ComponentSuppliers].[ControlID] = dbo.[FormControls].[ControlID]
                  )
				SELECT @@ROWCOUNT			
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormControls_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormControls_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormControls_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the FormControls table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormControls_Find
(

	@SearchUsingOR bit   = null ,

	@ControlId int   = null ,

	@Name nvarchar (100)  = null ,

	@ControlTypeId int   = null ,

	@ToolTipText nvarchar (1000)  = null ,

	@Border bit   = null ,

	@Top int   = null ,

	@Left int   = null ,

	@Width int   = null ,

	@Height int   = null ,

	@Font nvarchar (50)  = null ,

	@DefaultValue nvarchar (200)  = null ,

	@Visible bit   = null ,

	@Enable bit   = null ,

	@IsSummaryField bit   = null ,

	@IsCommon bit   = null ,

	@IsCode bit   = null ,

	@IsLayout bit   = null ,

	@ComponentId int   = null ,

	@FormId int   = null ,

	@FormType int   = null ,

	@Status int   = null ,

	@Description nvarchar (1000)  = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ControlID]
	, [Name]
	, [ControlTypeID]
	, [ToolTipText]
	, [Border]
	, [Top]
	, [Left]
	, [Width]
	, [Height]
	, [Font]
	, [DefaultValue]
	, [Visible]
	, [Enable]
	, [IsSummaryField]
	, [IsCommon]
	, [IsCode]
	, [IsLayout]
	, [ComponentID]
	, [FormID]
	, [FormType]
	, [Status]
	, [Description]
    FROM
	[dbo].[FormControls]
    WHERE 
	 ([ControlID] = @ControlId OR @ControlId IS NULL)
	AND ([Name] = @Name OR @Name IS NULL)
	AND ([ControlTypeID] = @ControlTypeId OR @ControlTypeId IS NULL)
	AND ([ToolTipText] = @ToolTipText OR @ToolTipText IS NULL)
	AND ([Border] = @Border OR @Border IS NULL)
	AND ([Top] = @Top OR @Top IS NULL)
	AND ([Left] = @Left OR @Left IS NULL)
	AND ([Width] = @Width OR @Width IS NULL)
	AND ([Height] = @Height OR @Height IS NULL)
	AND ([Font] = @Font OR @Font IS NULL)
	AND ([DefaultValue] = @DefaultValue OR @DefaultValue IS NULL)
	AND ([Visible] = @Visible OR @Visible IS NULL)
	AND ([Enable] = @Enable OR @Enable IS NULL)
	AND ([IsSummaryField] = @IsSummaryField OR @IsSummaryField IS NULL)
	AND ([IsCommon] = @IsCommon OR @IsCommon IS NULL)
	AND ([IsCode] = @IsCode OR @IsCode IS NULL)
	AND ([IsLayout] = @IsLayout OR @IsLayout IS NULL)
	AND ([ComponentID] = @ComponentId OR @ComponentId IS NULL)
	AND ([FormID] = @FormId OR @FormId IS NULL)
	AND ([FormType] = @FormType OR @FormType IS NULL)
	AND ([Status] = @Status OR @Status IS NULL)
	AND ([Description] = @Description OR @Description IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ControlID]
	, [Name]
	, [ControlTypeID]
	, [ToolTipText]
	, [Border]
	, [Top]
	, [Left]
	, [Width]
	, [Height]
	, [Font]
	, [DefaultValue]
	, [Visible]
	, [Enable]
	, [IsSummaryField]
	, [IsCommon]
	, [IsCode]
	, [IsLayout]
	, [ComponentID]
	, [FormID]
	, [FormType]
	, [Status]
	, [Description]
    FROM
	[dbo].[FormControls]
    WHERE 
	 ([ControlID] = @ControlId AND @ControlId is not null)
	OR ([Name] = @Name AND @Name is not null)
	OR ([ControlTypeID] = @ControlTypeId AND @ControlTypeId is not null)
	OR ([ToolTipText] = @ToolTipText AND @ToolTipText is not null)
	OR ([Border] = @Border AND @Border is not null)
	OR ([Top] = @Top AND @Top is not null)
	OR ([Left] = @Left AND @Left is not null)
	OR ([Width] = @Width AND @Width is not null)
	OR ([Height] = @Height AND @Height is not null)
	OR ([Font] = @Font AND @Font is not null)
	OR ([DefaultValue] = @DefaultValue AND @DefaultValue is not null)
	OR ([Visible] = @Visible AND @Visible is not null)
	OR ([Enable] = @Enable AND @Enable is not null)
	OR ([IsSummaryField] = @IsSummaryField AND @IsSummaryField is not null)
	OR ([IsCommon] = @IsCommon AND @IsCommon is not null)
	OR ([IsCode] = @IsCode AND @IsCode is not null)
	OR ([IsLayout] = @IsLayout AND @IsLayout is not null)
	OR ([ComponentID] = @ComponentId AND @ComponentId is not null)
	OR ([FormID] = @FormId AND @FormId is not null)
	OR ([FormType] = @FormType AND @FormType is not null)
	OR ([Status] = @Status AND @Status is not null)
	OR ([Description] = @Description AND @Description is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the ControlListItems table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_Get_List

AS


				
				SELECT
					[FormListID],
					[ControlID],
					[ItemData],
					[Status]
				FROM
					[dbo].[ControlListItems]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the ControlListItems table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [FormListID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([FormListID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [FormListID]'
				SET @SQL = @SQL + ' FROM [dbo].[ControlListItems]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[FormListID], O.[ControlID], O.[ItemData], O.[Status]
				FROM
				    [dbo].[ControlListItems] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[FormListID] = PageIndex.[FormListID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ControlListItems]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the ControlListItems table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_Insert
(

	@FormListId int    OUTPUT,

	@ControlId int   ,

	@ItemData nvarchar (200)  ,

	@Status int   
)
AS


				
				INSERT INTO [dbo].[ControlListItems]
					(
					[ControlID]
					,[ItemData]
					,[Status]
					)
				VALUES
					(
					@ControlId
					,@ItemData
					,@Status
					)
				
				-- Get the identity value
				SET @FormListId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the ControlListItems table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_Update
(

	@FormListId int   ,

	@ControlId int   ,

	@ItemData nvarchar (200)  ,

	@Status int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ControlListItems]
				SET
					[ControlID] = @ControlId
					,[ItemData] = @ItemData
					,[Status] = @Status
				WHERE
[FormListID] = @FormListId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the ControlListItems table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_Delete
(

	@FormListId int   
)
AS


				DELETE FROM [dbo].[ControlListItems] WITH (ROWLOCK) 
				WHERE
					[FormListID] = @FormListId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_GetByControlId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_GetByControlId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_GetByControlId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the ControlListItems table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_GetByControlId
(

	@ControlId int   
)
AS


				SELECT
					[FormListID],
					[ControlID],
					[ItemData],
					[Status]
				FROM
					[dbo].[ControlListItems]
				WHERE
					[ControlID] = @ControlId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_GetByFormListId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_GetByFormListId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_GetByFormListId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the ControlListItems table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_GetByFormListId
(

	@FormListId int   
)
AS


				SELECT
					[FormListID],
					[ControlID],
					[ItemData],
					[Status]
				FROM
					[dbo].[ControlListItems]
				WHERE
					[FormListID] = @FormListId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ControlListItems_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ControlListItems_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ControlListItems_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the ControlListItems table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ControlListItems_Find
(

	@SearchUsingOR bit   = null ,

	@FormListId int   = null ,

	@ControlId int   = null ,

	@ItemData nvarchar (200)  = null ,

	@Status int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [FormListID]
	, [ControlID]
	, [ItemData]
	, [Status]
    FROM
	[dbo].[ControlListItems]
    WHERE 
	 ([FormListID] = @FormListId OR @FormListId IS NULL)
	AND ([ControlID] = @ControlId OR @ControlId IS NULL)
	AND ([ItemData] = @ItemData OR @ItemData IS NULL)
	AND ([Status] = @Status OR @Status IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [FormListID]
	, [ControlID]
	, [ItemData]
	, [Status]
    FROM
	[dbo].[ControlListItems]
    WHERE 
	 ([FormListID] = @FormListId AND @FormListId is not null)
	OR ([ControlID] = @ControlId AND @ControlId is not null)
	OR ([ItemData] = @ItemData AND @ItemData is not null)
	OR ([Status] = @Status AND @Status is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the Suppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_Get_List

AS


				
				SELECT
					[SupplierID],
					[SupplierName],
					[Status]
				FROM
					[dbo].[Suppliers]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the Suppliers table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [SupplierID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([SupplierID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [SupplierID]'
				SET @SQL = @SQL + ' FROM [dbo].[Suppliers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[SupplierID], O.[SupplierName], O.[Status]
				FROM
				    [dbo].[Suppliers] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[SupplierID] = PageIndex.[SupplierID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Suppliers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the Suppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_Insert
(

	@SupplierId int    OUTPUT,

	@SupplierName nvarchar (200)  ,

	@Status int   
)
AS


				
				INSERT INTO [dbo].[Suppliers]
					(
					[SupplierName]
					,[Status]
					)
				VALUES
					(
					@SupplierName
					,@Status
					)
				
				-- Get the identity value
				SET @SupplierId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the Suppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_Update
(

	@SupplierId int   ,

	@SupplierName nvarchar (200)  ,

	@Status int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Suppliers]
				SET
					[SupplierName] = @SupplierName
					,[Status] = @Status
				WHERE
[SupplierID] = @SupplierId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the Suppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_Delete
(

	@SupplierId int   
)
AS


				DELETE FROM [dbo].[Suppliers] WITH (ROWLOCK) 
				WHERE
					[SupplierID] = @SupplierId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_GetBySupplierId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_GetBySupplierId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_GetBySupplierId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the Suppliers table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_GetBySupplierId
(

	@SupplierId int   
)
AS


				SELECT
					[SupplierID],
					[SupplierName],
					[Status]
				FROM
					[dbo].[Suppliers]
				WHERE
					[SupplierID] = @SupplierId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_GetByControlIdFromComponentSupplier procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_GetByControlIdFromComponentSupplier') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_GetByControlIdFromComponentSupplier
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records through a junction table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_GetByControlIdFromComponentSupplier
(

	@ControlId int   
)
AS


SELECT dbo.[Suppliers].[SupplierID]
       ,dbo.[Suppliers].[SupplierName]
       ,dbo.[Suppliers].[Status]
  FROM dbo.[Suppliers]
 WHERE EXISTS (SELECT 1
                 FROM dbo.[ComponentSuppliers] 
                WHERE dbo.[ComponentSuppliers].[ControlID] = @ControlId
                  AND dbo.[ComponentSuppliers].[SupplierID] = dbo.[Suppliers].[SupplierID]
                  )
				SELECT @@ROWCOUNT			
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Suppliers_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Suppliers_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Suppliers_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the Suppliers table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Suppliers_Find
(

	@SearchUsingOR bit   = null ,

	@SupplierId int   = null ,

	@SupplierName nvarchar (200)  = null ,

	@Status int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [SupplierID]
	, [SupplierName]
	, [Status]
    FROM
	[dbo].[Suppliers]
    WHERE 
	 ([SupplierID] = @SupplierId OR @SupplierId IS NULL)
	AND ([SupplierName] = @SupplierName OR @SupplierName IS NULL)
	AND ([Status] = @Status OR @Status IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [SupplierID]
	, [SupplierName]
	, [Status]
    FROM
	[dbo].[Suppliers]
    WHERE 
	 ([SupplierID] = @SupplierId AND @SupplierId is not null)
	OR ([SupplierName] = @SupplierName AND @SupplierName is not null)
	OR ([Status] = @Status AND @Status is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the Orders table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Get_List

AS


				
				SELECT
					[OrderID],
					[OrderType],
					[FormID],
					[CreatedDate]
				FROM
					[dbo].[Orders]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the Orders table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [OrderID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([OrderID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [OrderID]'
				SET @SQL = @SQL + ' FROM [dbo].[Orders]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[OrderID], O.[OrderType], O.[FormID], O.[CreatedDate]
				FROM
				    [dbo].[Orders] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[OrderID] = PageIndex.[OrderID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[Orders]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the Orders table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Insert
(

	@OrderId int    OUTPUT,

	@OrderType int   ,

	@FormId int   ,

	@CreatedDate datetime   
)
AS


				
				INSERT INTO [dbo].[Orders]
					(
					[OrderType]
					,[FormID]
					,[CreatedDate]
					)
				VALUES
					(
					@OrderType
					,@FormId
					,@CreatedDate
					)
				
				-- Get the identity value
				SET @OrderId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the Orders table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Update
(

	@OrderId int   ,

	@OrderType int   ,

	@FormId int   ,

	@CreatedDate datetime   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[Orders]
				SET
					[OrderType] = @OrderType
					,[FormID] = @FormId
					,[CreatedDate] = @CreatedDate
				WHERE
[OrderID] = @OrderId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the Orders table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Delete
(

	@OrderId int   
)
AS


				DELETE FROM [dbo].[Orders] WITH (ROWLOCK) 
				WHERE
					[OrderID] = @OrderId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_GetByFormId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_GetByFormId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_GetByFormId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the Orders table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_GetByFormId
(

	@FormId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[OrderID],
					[OrderType],
					[FormID],
					[CreatedDate]
				FROM
					[dbo].[Orders]
				WHERE
					[FormID] = @FormId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_GetByOrderId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_GetByOrderId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_GetByOrderId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the Orders table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_GetByOrderId
(

	@OrderId int   
)
AS


				SELECT
					[OrderID],
					[OrderType],
					[FormID],
					[CreatedDate]
				FROM
					[dbo].[Orders]
				WHERE
					[OrderID] = @OrderId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.Orders_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.Orders_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.Orders_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the Orders table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.Orders_Find
(

	@SearchUsingOR bit   = null ,

	@OrderId int   = null ,

	@OrderType int   = null ,

	@FormId int   = null ,

	@CreatedDate datetime   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [OrderID]
	, [OrderType]
	, [FormID]
	, [CreatedDate]
    FROM
	[dbo].[Orders]
    WHERE 
	 ([OrderID] = @OrderId OR @OrderId IS NULL)
	AND ([OrderType] = @OrderType OR @OrderType IS NULL)
	AND ([FormID] = @FormId OR @FormId IS NULL)
	AND ([CreatedDate] = @CreatedDate OR @CreatedDate IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [OrderID]
	, [OrderType]
	, [FormID]
	, [CreatedDate]
    FROM
	[dbo].[Orders]
    WHERE 
	 ([OrderID] = @OrderId AND @OrderId is not null)
	OR ([OrderType] = @OrderType AND @OrderType is not null)
	OR ([FormID] = @FormId AND @FormId is not null)
	OR ([CreatedDate] = @CreatedDate AND @CreatedDate is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the FormData table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_Get_List

AS


				
				SELECT
					[DataID],
					[Code],
					[FormID],
					[ControlID],
					[Data],
					[SupOrderCode],
					[FormType]
				FROM
					[dbo].[FormData]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the FormData table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [DataID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([DataID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [DataID]'
				SET @SQL = @SQL + ' FROM [dbo].[FormData]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[DataID], O.[Code], O.[FormID], O.[ControlID], O.[Data], O.[SupOrderCode], O.[FormType]
				FROM
				    [dbo].[FormData] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[DataID] = PageIndex.[DataID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[FormData]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the FormData table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_Insert
(

	@DataId int    OUTPUT,

	@Code int   ,

	@FormId int   ,

	@ControlId int   ,

	@Data nvarchar (200)  ,

	@SupOrderCode int   ,

	@FormType int   
)
AS


				
				INSERT INTO [dbo].[FormData]
					(
					[Code]
					,[FormID]
					,[ControlID]
					,[Data]
					,[SupOrderCode]
					,[FormType]
					)
				VALUES
					(
					@Code
					,@FormId
					,@ControlId
					,@Data
					,@SupOrderCode
					,@FormType
					)
				
				-- Get the identity value
				SET @DataId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the FormData table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_Update
(

	@DataId int   ,

	@Code int   ,

	@FormId int   ,

	@ControlId int   ,

	@Data nvarchar (200)  ,

	@SupOrderCode int   ,

	@FormType int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[FormData]
				SET
					[Code] = @Code
					,[FormID] = @FormId
					,[ControlID] = @ControlId
					,[Data] = @Data
					,[SupOrderCode] = @SupOrderCode
					,[FormType] = @FormType
				WHERE
[DataID] = @DataId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the FormData table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_Delete
(

	@DataId int   
)
AS


				DELETE FROM [dbo].[FormData] WITH (ROWLOCK) 
				WHERE
					[DataID] = @DataId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_GetBySupOrderCode procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_GetBySupOrderCode') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_GetBySupOrderCode
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormData table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_GetBySupOrderCode
(

	@SupOrderCode int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[DataID],
					[Code],
					[FormID],
					[ControlID],
					[Data],
					[SupOrderCode],
					[FormType]
				FROM
					[dbo].[FormData]
				WHERE
					[SupOrderCode] = @SupOrderCode
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_GetByCode procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_GetByCode') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_GetByCode
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormData table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_GetByCode
(

	@Code int   
)
AS


				SELECT
					[DataID],
					[Code],
					[FormID],
					[ControlID],
					[Data],
					[SupOrderCode],
					[FormType]
				FROM
					[dbo].[FormData]
				WHERE
					[Code] = @Code
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_GetByControlId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_GetByControlId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_GetByControlId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormData table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_GetByControlId
(

	@ControlId int   
)
AS


				SELECT
					[DataID],
					[Code],
					[FormID],
					[ControlID],
					[Data],
					[SupOrderCode],
					[FormType]
				FROM
					[dbo].[FormData]
				WHERE
					[ControlID] = @ControlId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_GetByFormId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_GetByFormId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_GetByFormId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormData table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_GetByFormId
(

	@FormId int   
)
AS


				SELECT
					[DataID],
					[Code],
					[FormID],
					[ControlID],
					[Data],
					[SupOrderCode],
					[FormType]
				FROM
					[dbo].[FormData]
				WHERE
					[FormID] = @FormId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_GetByDataId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_GetByDataId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_GetByDataId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the FormData table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_GetByDataId
(

	@DataId int   
)
AS


				SELECT
					[DataID],
					[Code],
					[FormID],
					[ControlID],
					[Data],
					[SupOrderCode],
					[FormType]
				FROM
					[dbo].[FormData]
				WHERE
					[DataID] = @DataId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.FormData_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.FormData_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.FormData_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the FormData table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.FormData_Find
(

	@SearchUsingOR bit   = null ,

	@DataId int   = null ,

	@Code int   = null ,

	@FormId int   = null ,

	@ControlId int   = null ,

	@Data nvarchar (200)  = null ,

	@SupOrderCode int   = null ,

	@FormType int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [DataID]
	, [Code]
	, [FormID]
	, [ControlID]
	, [Data]
	, [SupOrderCode]
	, [FormType]
    FROM
	[dbo].[FormData]
    WHERE 
	 ([DataID] = @DataId OR @DataId IS NULL)
	AND ([Code] = @Code OR @Code IS NULL)
	AND ([FormID] = @FormId OR @FormId IS NULL)
	AND ([ControlID] = @ControlId OR @ControlId IS NULL)
	AND ([Data] = @Data OR @Data IS NULL)
	AND ([SupOrderCode] = @SupOrderCode OR @SupOrderCode IS NULL)
	AND ([FormType] = @FormType OR @FormType IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [DataID]
	, [Code]
	, [FormID]
	, [ControlID]
	, [Data]
	, [SupOrderCode]
	, [FormType]
    FROM
	[dbo].[FormData]
    WHERE 
	 ([DataID] = @DataId AND @DataId is not null)
	OR ([Code] = @Code AND @Code is not null)
	OR ([FormID] = @FormId AND @FormId is not null)
	OR ([ControlID] = @ControlId AND @ControlId is not null)
	OR ([Data] = @Data AND @Data is not null)
	OR ([SupOrderCode] = @SupOrderCode AND @SupOrderCode is not null)
	OR ([FormType] = @FormType AND @FormType is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets all records from the ComponentSuppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_Get_List

AS


				
				SELECT
					[ControlID],
					[SupplierID]
				FROM
					[dbo].[ComponentSuppliers]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Gets records from the ComponentSuppliers table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [ControlID] int, [SupplierID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([ControlID], [SupplierID])'
				SET @SQL = @SQL + ' SELECT'
				SET @SQL = @SQL + ' [ControlID], [SupplierID]'
				SET @SQL = @SQL + ' FROM [dbo].[ComponentSuppliers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Only get the number of rows needed here.
				SET ROWCOUNT @PageUpperBound
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Reset Rowcount back to all
				SET ROWCOUNT 0
				
				-- Return paged results
				SELECT O.[ControlID], O.[SupplierID]
				FROM
				    [dbo].[ComponentSuppliers] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[ControlID] = PageIndex.[ControlID]
					AND O.[SupplierID] = PageIndex.[SupplierID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ComponentSuppliers]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Inserts a record into the ComponentSuppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_Insert
(

	@ControlId int   ,

	@SupplierId int   
)
AS


				
				INSERT INTO [dbo].[ComponentSuppliers]
					(
					[ControlID]
					,[SupplierID]
					)
				VALUES
					(
					@ControlId
					,@SupplierId
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Updates a record in the ComponentSuppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_Update
(

	@ControlId int   ,

	@OriginalControlId int   ,

	@SupplierId int   ,

	@OriginalSupplierId int   
)
AS


				
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ComponentSuppliers]
				SET
					[ControlID] = @ControlId
					,[SupplierID] = @SupplierId
				WHERE
[ControlID] = @OriginalControlId 
AND [SupplierID] = @OriginalSupplierId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Deletes a record in the ComponentSuppliers table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_Delete
(

	@ControlId int   ,

	@SupplierId int   
)
AS


				DELETE FROM [dbo].[ComponentSuppliers] WITH (ROWLOCK) 
				WHERE
					[ControlID] = @ControlId
					AND [SupplierID] = @SupplierId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_GetByControlId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_GetByControlId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_GetByControlId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the ComponentSuppliers table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_GetByControlId
(

	@ControlId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ControlID],
					[SupplierID]
				FROM
					[dbo].[ComponentSuppliers]
				WHERE
					[ControlID] = @ControlId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_GetBySupplierId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_GetBySupplierId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_GetBySupplierId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the ComponentSuppliers table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_GetBySupplierId
(

	@SupplierId int   
)
AS


				SET ANSI_NULLS OFF
				
				SELECT
					[ControlID],
					[SupplierID]
				FROM
					[dbo].[ComponentSuppliers]
				WHERE
					[SupplierID] = @SupplierId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_GetByControlIdSupplierId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_GetByControlIdSupplierId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_GetByControlIdSupplierId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Select records from the ComponentSuppliers table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_GetByControlIdSupplierId
(

	@ControlId int   ,

	@SupplierId int   
)
AS


				SELECT
					[ControlID],
					[SupplierID]
				FROM
					[dbo].[ComponentSuppliers]
				WHERE
					[ControlID] = @ControlId
					AND [SupplierID] = @SupplierId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ComponentSuppliers_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ComponentSuppliers_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ComponentSuppliers_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By: N2 Corporation (http://www.n2.com)
-- Purpose: Finds records in the ComponentSuppliers table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ComponentSuppliers_Find
(

	@SearchUsingOR bit   = null ,

	@ControlId int   = null ,

	@SupplierId int   = null 
)
AS


				
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [ControlID]
	, [SupplierID]
    FROM
	[dbo].[ComponentSuppliers]
    WHERE 
	 ([ControlID] = @ControlId OR @ControlId IS NULL)
	AND ([SupplierID] = @SupplierId OR @SupplierId IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [ControlID]
	, [SupplierID]
    FROM
	[dbo].[ComponentSuppliers]
    WHERE 
	 ([ControlID] = @ControlId AND @ControlId is not null)
	OR ([SupplierID] = @SupplierId AND @SupplierId is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

